$(function(){
	//		获取到cookie
	var cookieName = $.cookie('username')
	console.log(cookieName)
	if(cookieName){
//		onerror当文件加载失败时会触发这个事件
		  var header = '<img src="uploads/' + $.cookie('header') + '" onerror="this.src=\'images/user.png\'">'
        $('#user').empty().html(header + cookieName)
	}
	//	退出删除cookie
	$('.dropdown-menu li').last().click(function(){
		$.removeCookie('username')
		location.reload()
	})
	
	$('#user').click(function(){
		if(!cookieName){
			$(this).removeAttr('data-toggle')
			location.href = '/login.html'
		}
	})
	
	
	function checkNumber(){
		var $val = $('textarea').val().length
		$('textarea').next().text(140 -$val + '/140')
	}
	var timer
	$('textarea').focus(function(){
		timer = setInterval(checkNumber,20)
	})
	$('textarea').blur(function(){
		clearInterval(timer)
	})
//	发起请求
	$('form').submit(function(ev){
		ev.preventDefault()
		var data = $(this).serialize()
		$.post('/answer',data).done(function(data){
			$('.modal-body p').text(data.message)
			$('.modal').modal('show')
			if(data.code == 'success'){
				$('.modal').on('hidden.bs.modal',function(){
					location.href = '/'
				})
			}
		})
	})
})
